package com.pan.listener.impl;

import com.github.kwhat.jnativehook.NativeHookException;
import com.pan.action.MyAction;
import com.pan.listener.CommonListener;
import com.pan.listener.MyListener;

import java.awt.event.ActionListener;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

public class SerializationSavePressedListener extends CommonListener {

    private Set<String> currentPressedKey;{
        currentPressedKey = new HashSet<>();
    }

    public SerializationSavePressedListener(){
    }


    @Override
    public void pressed(String key) {
        currentPressedKey.add(key);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(currentPressedKey);
            objectOutputStream.flush();
            byte[] serializationBuf = byteArrayOutputStream.toByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializationBuf);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Set<String> newSet = (Set<String>) objectInputStream.readObject();
            super.action.hook(newSet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void released(String key) {
        currentPressedKey.remove(key);
    }


    public static void main(String[] args) throws NativeHookException {
//        GlobalScreen.registerNativeHook();
//        GlobalKeyListener keyListener1 = new GlobalKeyListener(new GlobalQuitAction());
//        GlobalKeyListener keyListener2 = new GlobalKeyListener(new MarkMouseAction());
//        GlobalScreen.addNativeKeyListener(keyListener1);
//        GlobalScreen.addNativeKeyListener(keyListener2);
    }
}
